<?php
/**
 * Created by PhpStorm.
 * User: a
 * Date: 2016/6/7
 * Time: 17:31
 */
namespace Admin\Controller\Report;
use Think\Controller;

class ApplyreportController extends Controller{

    /**
     * 贷款信息申请报文table
     */
    public function apply_report(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = strtotime($month_time."-01");
        $end_time = strtotime("+1 month",$start_time)-1;
        $borrow_status=I("post.borrow_status","");
        $select_name = I("post.select_name","");
        $report=D("Report");
        $report_array=$report->get_borrow_apply_info($start_time,$end_time,$borrow_status,$select_name);
        $json=json_encode($report_array);
        $this->assign('report_json',$json);

        $borrow=D("Borrowinfo");
        $borrow_status_array=$borrow->borrow_status();
        $this->assign('borrow_status',$borrow_status_array);

        $search_array['month_time']=$month_time;
        $search_array['borrow_status']=$borrow_status;
        $search_array['select_name']=$select_name;
        $this->assign("search_array",$search_array);
        $this->display();
    }

    /**
     * 贷款信息申请报文导出
     */
    public function apply_report_export(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = strtotime($month_time."-01");
        $end_time = strtotime("+1 month",$start_time)-1;
        $borrow_status=I("post.borrow_status","");
        $select_name=I("post.select_name","");

        $report=D("Report");
        $report_array=$report->get_borrow_apply_info($start_time,$end_time,$borrow_status,$select_name);
        $count=count($report_array);

        $info_type=C("apply_info_type");//信息类别
        $org_code=C("org_code");

        $number_array=C("number_array");
        $flowing_water="";
        for($j=0;$j<3;$j++){
            $k=rand(0,36);
            $flowing_water.=$number_array[$k];
        }
        $dir=$org_code.date("Ymd",time()).$flowing_water."1";
        $file_name=$dir.".txt";
        $path="Public/file/".$file_name;
        $file=fopen($path,"a+");

        $identity_type=0;//身份证类型：0代表身份证
        $apply_type=91;//贷款申请类型：91代表个人消费贷款
        for($i=0;$i<$count;$i++){
            $borrow_id=$report_array[$i]["contract_id"];
            $cust_name=$report_array[$i]['cust_name'];
            if($cust_name==''){
                continue;
            }
            $cust_name=iconv("UTF-8","gbk//TRANSLIT",$cust_name);
            $identity_number=$report_array[$i]["cust_identity"];
            $borrow_money=$report_array[$i]['borrow_money'];
            $borrow_period=$report_array[$i]['borrow_loanperiod'];
            $borrow_createdate=$report_array[$i]['borrow_createdate'];
            $borrow_stateid=$report_array[$i]['borrow_stateid'];
            if($borrow_stateid==1||$borrow_stateid==7||$borrow_stateid==5){
                $borrow_stateid=1;
            }elseif($borrow_stateid==2){
                $borrow_stateid=2;
            }else{
                $borrow_stateid=3;
            }
            $file_string=$info_type.$org_code.str_pad($borrow_id,40," ",STR_PAD_RIGHT).str_pad($cust_name,30," ",STR_PAD_RIGHT).
                str_pad($identity_type,1," ",STR_PAD_RIGHT). str_pad($identity_number,18," ",STR_PAD_RIGHT).str_pad($apply_type,1," ",STR_PAD_RIGHT).
                str_pad($borrow_money,10,"0",STR_PAD_LEFT). str_pad(str_pad($borrow_period,2,"0",STR_PAD_LEFT),6," ",STR_PAD_RIGHT).str_pad($borrow_createdate,8," ",STR_PAD_RIGHT)
                .str_pad($borrow_stateid,1," ",STR_PAD_RIGHT);
            fwrite($file,$file_string."\r\n");
        }
        $this->ajaxReturn($file_name);
    }
    /**
     * 贷款信息申请报文导出  excel格式
     */
    public function apply_report_export_excel(){
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel  = new \PHPExcel();

        $start_time=I("post.start_time","");
        $end_time=I("post.end_time","");
        $borrow_status=I("post.borrow_status","");
        $select_name=I("post.select_name","");

        $report=D("Report");
        $report_array=$report->get_borrow_apply_info($start_time,$end_time,$borrow_status,$select_name);
        $count=count($report_array);

        $info_type=C("apply_info_type");//信息类别
        $org_code=C("org_code");

        $number_array=C("number_array");
        $flowing_water="";
        for($j=0;$j<3;$j++){
            $k=rand(0,36);
            $flowing_water.=$number_array[$k];
        }
        $dir=$org_code.date("Ymd",time()).$flowing_water."1";
        $file_name=$dir;
        $path="Public/file/".$file_name;
        $objPHPExcel->getProperties()->setCreator("转弯的阳光")
            ->setLastModifiedBy("转弯的阳光")
            ->setTitle("数据EXCEL导出")
            ->setSubject("数据EXCEL导出")
            ->setDescription("备份数据")
            ->setKeywords("excel")
            ->setCategory("result file");

        $identity_type=0;//身份证类型：0代表身份证
        $apply_type=91;//贷款申请类型：91代表个人消费贷款
        for($i=0;$i<$count;$i++){
            $borrow_id=$report_array[$i]["borrow_id"];
            $cust_name=$report_array[$i]['cust_name'];
            if($cust_name==''){
                continue;
            }
            $cust_name=iconv("UTF-8","gbk//TRANSLIT",$cust_name);
            $identity_number=$report_array[$i]["cust_identity"];
            $borrow_money=$report_array[$i]['borrow_money'];
            $borrow_period=$report_array[$i]['borrow_loanperiod'];
            $borrow_createdate=$report_array[$i]['borrow_createdate'];
            $borrow_stateid=$report_array[$i]['borrow_stateid'];
            if($borrow_stateid==1||$borrow_stateid==7||$borrow_stateid==5){
                $borrow_stateid=1;
            }elseif($borrow_stateid==2){
                $borrow_stateid=2;
            }else{
                $borrow_stateid=3;
            }
            $file_string=$info_type.$org_code.str_pad($borrow_id,40," ",STR_PAD_RIGHT).str_pad($cust_name,30," ",STR_PAD_RIGHT).
                str_pad($identity_type,1," ",STR_PAD_RIGHT). str_pad($identity_number,18," ",STR_PAD_RIGHT).str_pad($apply_type,1," ",STR_PAD_RIGHT).
                str_pad($borrow_money,10,"0",STR_PAD_LEFT). str_pad($borrow_period,6," ",STR_PAD_RIGHT).str_pad($borrow_createdate,8," ",STR_PAD_RIGHT)
                .str_pad($borrow_stateid,1," ",STR_PAD_RIGHT);
            $file_string=iconv("gbk//TRANSLIT","UTF-8",$file_string);
            $objPHPExcel->setActiveSheetIndex(0)
                //Excel的第A列，uid是你查出数组的键值，下面以此类推
                ->setCellValue('A'.($i+1), $file_string);
        }
        $objPHPExcel->getActiveSheet()->setTitle('User');
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$file_name.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        $this->ajaxReturn($file_name);
    }

    public function apply_unline(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = strtotime($month_time."-01");
        $end_time = strtotime("+1 month",$start_time)-1;
        $borrow_status=I("post.borrow_status","");
        $select_name=I("post.select_name","");
        $report=D("Report");
        $report_array=$report->get_apply_unline($start_time,$end_time,$borrow_status,$select_name);
        $json=json_encode($report_array);
        $this->assign('report_json',$json);
        $search_array['month_time']=$month_time;
        $search_array['borrow_status']=$borrow_status;
        $search_array['select_name']=$select_name;
        $this->assign("search_array",$search_array);

        $borrow=D("Borrowinfo");
        $borrow_status_array=$borrow->borrow_status();
        $this->assign('borrow_status',$borrow_status_array);

        $this->display();
    }

    /**
     * 新增或修改线下借款申请报文
     */
    public function apply_unline_insert(){
        $data=I("post.data");
        $report=D("Report");
        $data_count=count($data);
        if($data_count>0) {
            for($i=0;$i<$data_count;$i++) {
                $borrow_id = $data[$i]['borrow_id'];
                $count = $report->select_one_apply_unline($borrow_id);
                if ($count != 0) {
                    $result = $report->update_apply_unline($data[$i]);
                } else {
                    $result = $report->insert_apply_unline($data[$i]);
                }
            }
        }else{
            $result=2;
        }
        $this->ajaxReturn($result);
    }

    public function update_contract(){
        $data=I("post.data");
        $report=D("Report");
        $data_count=count($data);
        $result=0;
        if($data_count>0){
            for($i=0;$i<$data_count;$i++){
                $result+=$report->update_contract($data[$i]);
            }
        }
        $this->ajaxReturn($result);
    }

    public function apply_unline_export(){
        $month_time=date("Y-m",strtotime("-1 month",time()));
        $month_time = I("post.month_time",$month_time);
        $start_time = $month_time."-01";
        $end_time = date("Y-m-d H:i:s",strtotime("+1 month",$start_time)-1);
        $borrow_status=I("post.borrow_status","");
        $select_name=I("post.select_name","");
        $report=D("Report");
        $report_array=$report->get_apply_unline($start_time,$end_time,$borrow_status,$select_name);

        $count=count($report_array);

        $info_type=C("apply_info_type");//信息类别
        $org_code=C("org_code");

        $number_array=C("number_array");
        $flowing_water="";
        for($j=0;$j<3;$j++){
            $k=rand(0,36);
            $flowing_water.=$number_array[$k];
        }
        $dir=$org_code.date("Ymd",time()).$flowing_water."1";
        $file_name=$dir.".txt";
        $path="Public/file/".$file_name;
        $file=fopen($path,"a+");


        for($i=0;$i<$count;$i++){
            $borrow_id=$report_array[$i]["borrow_id"];
            $cust_name=$report_array[$i]['cust_name'];
            if($cust_name==''){
                continue;
            }
            $identity_type=$report_array[$i]['identity_type'];
            $cust_name=iconv("UTF-8","gbk//TRANSLIT",$cust_name);
            $identity_number=$report_array[$i]["identity_number"];
            $apply_type=$report_array[$i]["apply_type"];
            $borrow_money=$report_array[$i]['apply_money'];
            $borrow_period=$report_array[$i]['apply_period'];
            $borrow_createdate=$report_array[$i]['apply_date'];
            $borrow_stateid=$report_array[$i]['apply_status'];
            $file_string=$info_type.$org_code.str_pad($borrow_id,40," ",STR_PAD_RIGHT).str_pad($cust_name,30," ",STR_PAD_RIGHT).
                str_pad($identity_type,1," ",STR_PAD_RIGHT). str_pad($identity_number,18," ",STR_PAD_RIGHT).str_pad($apply_type,1," ",STR_PAD_RIGHT).
                str_pad($borrow_money,10,"0",STR_PAD_LEFT). str_pad($borrow_period,6," ",STR_PAD_RIGHT).str_pad($borrow_createdate,8," ",STR_PAD_RIGHT)
                .str_pad($borrow_stateid,1," ",STR_PAD_RIGHT);
            fwrite($file,$file_string."\r\n");
        }
        $this->ajaxReturn($file_name);
    }

}